/*
Filename:    association_collection.h
Author:      Erik Öjebo
Date:        2009-01-10
Description: A collection of SCTP association id:s.
             Adds new id:s and removes old id:s by handling SCTP_ASSOC_CHANGE
             notifications.
Notes:       To be able to update the collection, handleAssociationEvent(...)
             must be called with SCTP_ASSOC_CHANGE notification messages
             that are received on the socket.
             If an association is peeled off the socket, it must be removed
             manually by calling remove(...), since peeloff does not trigger
             any SCTP_ASSOC_CHANGE notifications.
*/

#ifndef ASSOCIATION_COLLECTION_H_
#define ASSOCIATION_COLLECTION_H_

#include <netinet/sctp.h>
#include <list>
#include "association_collection_const_iterator.h"

class AssociationCollectionConstIterator;

class AssociationCollection
{
public:
    // Checks if the notification is a sctp_assoc_change notification,
    // and if so, updates the collection accordingly.
    // If an association is closed or lost it is removed, and if an
    // association is created it is added to the collection.
    void handleAssociationEvent(union sctp_notification* notification);

    // Returns an iterator pointing to the first element of the collection
    AssociationCollectionConstIterator begin() const;

    // Force an association to be removed from the collection.
    // Normally the event handler takes care of this, but the user
    // should be able to remove an association anyway.
    // This should be called when an association is peeled off a socket,
    // since that does not trigger a assoc_change notification.
    void remove(sctp_assoc_t id);

    // Returns the number of associations in the collection
    int count() const;

private:
    // Returns iterators to the beginning and end of the list.
    // Only used by the custom iterator class, and because of that
    // they are private and the iterator class is a friend.
    std::list<sctp_assoc_t>::const_iterator listBegin() const;
    std::list<sctp_assoc_t>::const_iterator listEnd() const;

    friend class AssociationCollectionConstIterator;
    std::list<sctp_assoc_t> associations;
};

#endif
